package cn.net.cfss.fgbp.auth.config;

import cn.net.cfss.fgbp.base.constant.ErrCodeEnum;
import cn.net.cfss.fgbp.base.except.CodeException;
import cn.net.cfss.fgbp.base.vo.BaseRespVo;
import cn.net.cfss.fgbp.base.vo.ReturnVo;
import feign.RetryableException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class ExceptionConf {

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */

    @ExceptionHandler(value = Exception.class)
    public ReturnVo errorHandler(Exception ex) {
        log.error("{}", ExceptionUtils.getFullStackTrace(ex));
        return new ReturnVo(new BaseRespVo(ErrCodeEnum.ERR,ex.getMessage()));
    }
    @ExceptionHandler(value = RetryableException.class)
    public ReturnVo retryableExceptionHandler(RetryableException ex) {
        log.error("{}",ExceptionUtils.getFullStackTrace(ex));
        return new ReturnVo(new BaseRespVo(ErrCodeEnum.ERR,ex.getMessage()));
    }
    @ExceptionHandler({CodeException.class})
    public ReturnVo handle(CodeException e) {
        log.error("{}",e.getDetail());
        return new ReturnVo(new BaseRespVo(e.getErrCodeEnum(),e.getDetail()));
    }
}
